﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CDDocumentExtractor
{
    public partial class FolderListManager : Form
    {
        List<FolderEntity> folders;

        public FolderListManager()
        {
            InitializeComponent();
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            if (txtFolderPath.Text.Trim().Length <= 0)
            {
                MessageBox.Show("Please provide correct Folder path.");
                txtFolderPath.Focus();
                return;
            }

            cmdAdd.Enabled = false;

            using (Db4objects.Db4o.IObjectContainer db = DataAccess.GetDataContainer())
            {
                FolderEntity fld = null;
                if (folders != null)
                {
                    fld = db.Query<FolderEntity>(f => f.FolderPath.Equals(txtFolderPath.Text.Trim())).FirstOrDefault();
                }

                if (fld == null)
                {
                    fld = new FolderEntity();
                    if (folders.Count > 0)
                    {
                        fld.FolderID = (from c in folders select c.FolderID).Max() + 1;
                    }
                    else
                    {
                        fld.FolderID = 1;
                    }
                }

                fld.FolderPath = txtFolderPath.Text.Trim();
                fld.Processed = false;
                db.Store(fld);
                db.Commit();

                folders = db.Query<FolderEntity>().ToList();
                PopulateListView();
            }

            cmdAdd.Enabled = true;
        }

        private void FolderListManager_Load(object sender, EventArgs e)
        {
            using (Db4objects.Db4o.IObjectContainer db = DataAccess.GetDataContainer())
            {
                folders = db.Query<FolderEntity>().ToList();
            }
            CopyDocumentsFromFolderDataFile();

            PopulateListView();
        }

        public void PopulateListView()
        {
            //http://localhost:49152/NXT/gateway.dll/Manupatra/sc/1980-2000SC/sc2000/
            listView1.Items.Clear();

            foreach (var item in folders)
            {
                if (!item.Processed)
                {
                    var lvi = listView1.Items.Add(item.FolderID.ToString());
                    lvi.SubItems.Add(item.FolderPath);
                    lvi.SubItems.Add(item.Processed.ToString());
                }
            }
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                txtFolderPath.Text = listView1.SelectedItems[0].SubItems[1].Text;
            }
        }

        private void listView1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (listView1.SelectedItems.Count > 0)
                {
                    var fld = folders.Find(f => f.FolderID.ToString().Equals(listView1.SelectedItems[0].Text.Trim()));
                    if (fld != null)
                    {
                        using (Db4objects.Db4o.IObjectContainer db = DataAccess.GetDataContainer())
                        {
                            db.Delete(fld);
                            db.Commit();
                        }
                        //Reload Form
                        FolderListManager_Load(this, null);
                    }
                }
            }
        }

        private void CopyDocumentsFromFolderDataFile()
        {
            if (File.Exists(Path.Combine(Application.StartupPath, "FolderData.txt")))
            {
                var lines = File.ReadAllLines(Path.Combine(Application.StartupPath, "FolderData.txt"));
                using (Db4objects.Db4o.IObjectContainer db = DataAccess.GetDataContainer())
                {
                    foreach (var item in lines)
                    {
                        FolderEntity fld = null;
                        if (folders != null)
                        {
                            fld = db.Query<FolderEntity>(f => f.FolderPath.Equals(item.Trim())).FirstOrDefault();
                        }

                        if (fld == null)
                        {
                            fld = new FolderEntity();
                            if (folders.Count > 0)
                            {
                                fld.FolderID = (from c in folders select c.FolderID).Max() + 1;
                            }
                            else
                            {
                                fld.FolderID = 1;
                            }
                        }
                        fld.FolderPath = item.Trim();
                        fld.Processed = false;
                        folders.Add(fld);
                        db.Store(fld);
                    }
                    db.Commit();

                    folders = db.Query<FolderEntity>().ToList();
                }
            }
        }

    }
}
